package com.zhiyume.service;

import com.zhiyume.dao.api.TActivityMapper;
import com.zhiyume.dao.api.TActivitySelfMapper;
import com.zhiyume.dao.entity.TActivity;
import com.zhiyume.dao.entity.TActivityCriteria;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.service.util.GenerateKeyUtil;
import com.zhiyume.vo.CommonReqVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhiyume.util.TypeConstants.*;

/**
 *
 * @author wang
 * @version $Id: TActivityService.java, v 0.1 2019-01-25 11:19:47 wang Exp $$
 */

@Service
public class TActivityService {
    @Autowired
    private TActivityMapper tActivityMapper;

    @Autowired
    private TActivitySelfMapper tActivitySelfMapper;

    /**
     * 根据Id查询信息
     * @return
     */
    public TActivity findActivityById(long id){
        TActivityCriteria example = new TActivityCriteria();
        TActivityCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);
        List<TActivity> activityList = tActivityMapper.selectByExampleWithBLOBs(example);
        if(null != activityList && activityList.size() > 0) {
            return activityList.get(0);
        }

        return null;
    }


    /**
     * 添加信息
     * @param tActivity     * @return
     */
    public TActivity addActivity(TActivity tActivity) throws Exception{

        long id = GenerateKeyUtil.generationPK();
        tActivity.setId(id);
        tActivity.setCreateTime(DateUtil.getNow());

        tActivityMapper.insert(tActivity);
        
        return tActivity;
    }
    
    
    /**
     * 修改人员信息
     * @param tActivity     * @return
     */
    public int updateActivity(TActivity tActivity) {

        TActivityCriteria example = new TActivityCriteria();
        TActivityCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(tActivity.getId());
        return tActivityMapper.updateByExample(tActivity,example);
    }
    
     /**
     * 逻辑删除
     * @param id
     * @return
     */
    public int deleteActivity(long id) {
        TActivityCriteria example = new TActivityCriteria();
        TActivityCriteria.Criteria c = example.createCriteria();
        c.andIdEqualTo(id);

        return tActivityMapper.deleteByExample(example);
    }

     /**
     * 逻辑发布
     * @param id
     * @return
     */
    public int publishActivity(long id) {
        TActivity tActivity = findActivityById(id);
        tActivity.setPublishTime(DateUtil.getNow());
        tActivity.setUpdateTime(DateUtil.getNow());

        tActivity.setStatus(Integer.parseInt(activityStatusMap.get(ACTIVITY_PUBLISH).toString()));
        return updateActivity(tActivity);
    }

     /**
     * 逻辑拒绝
     * @param id
     * @return
     */
    public int refuseActivity(long id) {
        TActivity tActivity = findActivityById(id);
        tActivity.setUpdateTime(DateUtil.getNow());

        tActivity.setStatus(Integer.parseInt(activityStatusMap.get(ACTIVITY_REFUSE).toString()));
        return updateActivity(tActivity);
    }


     /**
     * 逻辑下架
     * @param id
     * @return
     */
    public void underOrUpActivity(long id,int isShow) {
        TActivity tActivity = findActivityById(id);
        tActivity.setUpdateTime(DateUtil.getNow());

        tActivity.setIsShow(isShow);
        updateActivity(tActivity);
    }


    public <T extends CommonReqVO> List<TActivity>  queryActivityList(T t, PageReq page) {
        TActivityCriteria tActivityCriteria = new TActivityCriteria();
        TActivityCriteria.Criteria criteria = tActivityCriteria.createCriteria();
        tActivityCriteria.setLimitStart(page.getPage()*page.getPageSize());
        tActivityCriteria.setLimitEnd(page.getPageSize());
        tActivityCriteria.setOrderByClause("create_time desc, publish_time desc");

        createCriteria(t, criteria);
        return tActivityMapper.selectByExample(tActivityCriteria);
    }
    /**
     * 获取推荐资讯列表(暂定为id与当前不同的,按id倒序取5个)
     * @param id
     * @param limit 限制获取的条数
     * @return
     */
    public List<TActivity> getActivityOtherList(long id, int limit){
        Map map = new HashMap();
        map.put("id",id);
        map.put("limit",limit);
        List<TActivity> list = tActivitySelfMapper.getActivityOtherList(map);
        return list;
    }



    public <T extends CommonReqVO> int countActivity(T dto) {
        TActivityCriteria tActivityCriteria = new TActivityCriteria();
        TActivityCriteria.Criteria criteria = tActivityCriteria.createCriteria();


        createCriteria(dto, criteria);
        return tActivityMapper.countByExample(tActivityCriteria);
    }
    
    private <T extends CommonReqVO> void createCriteria(T dto, TActivityCriteria.Criteria criteria) {

        if(dto.getDataId()!=-1){
            criteria.andIdEqualTo(dto.getDataId());
        }
        if(StringUtils.isNotBlank(dto.getTitle())){
            criteria.andTitleLike("%"+dto.getTitle()+"%");
        }
        if(StringUtils.isNotBlank(dto.getStatus())){
            criteria.andStatusEqualTo(Integer.valueOf(dto.getStatus()));
        }
        if(StringUtils.isNotBlank(dto.getStartTime())){
            criteria.andPublishTimeGreaterThan(dto.getStartTime());
        }
        if(StringUtils.isNotBlank(dto.getEndTime())){
            criteria.andPublishTimeLessThan(dto.getEndTime());
        }
                
    }

}
